package com.itheima.ydd.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.mapper.YddLinkMapper;
import com.itheima.ydd.service.IYddLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddLinkServiceImpl extends ServiceImpl<YddLinkMapper, YddLink> implements IYddLinkService {

    @Override
    public ResultData selectByPage(Integer type, Integer page, Integer limit) {
        Page<YddLink> page1 = new Page<>(page,limit);
        LambdaQueryWrapper<YddLink> wrapper = new LambdaQueryWrapper<>();
//        LambdaQueryWrapper<YddLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddLink::getType,type);
        Page<YddLink> page2 = page(page1, wrapper);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("count",page2.getCountId());
        map.put("totalPages",page2.getTotal());
        map.put("pageSize",page2.getSize());
        map.put("currentPage",page2.getCurrent());
        map.put("data",page2.getRecords());
        return ResultData.ok(null,map);
    }

    @Override
    public ResultData rowInfo(Integer id) {
        YddLink byId = getById(id);
        return ResultData.ok(null,byId);
    }

    @Override
    public void updateyddArt(YddLink yddLink) {
        try {
            updateById(yddLink);
        } catch (Exception e) {
            throw new BusinessException("修改失败");
        }
    }

    @Override
    public void add(YddLink yddLink) {
            try {
                save(yddLink);
            } catch (Exception e) {
                throw new BusinessException("新增失败");
            }
        }
}
